home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / GrowBoxDock / Sources / MovieFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-30  |  10.0 KB  |  302 lines

  1. /*
  2.     File:        MovieFile.c
  3.  
  4.     Contains:    Movie file support for simple text application
  5.  
  6.     Version:    Mac OS X
  7.  
  8.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  9.                 ("Apple") in consideration of your agreement to the following terms, and your
  10.                 use, installation, modification or redistribution of this Apple software
  11.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  12.                 please do not use, install, modify or redistribute this Apple software.
  13.  
  14.                 In consideration of your agreement to abide by the following terms, and subject
  15.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  16.                 copyrights in this original Apple software (the "Apple Software"), to use,
  17.                 reproduce, modify and redistribute the Apple Software, with or without
  18.                 modifications, in source and/or binary forms; provided that if you redistribute
  19.                 the Apple Software in its entirety and without modifications, you must retain
  20.                 this notice and the following text and disclaimers in all such redistributions of
  21.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  22.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  23.                 Apple Software without specific prior written permission from Apple.  Except as
  24.                 expressly stated in this notice, no other rights or licenses, express or implied,
  25.                 are granted by Apple herein, including but not limited to any patent rights that
  26.                 may be infringed by your derivative works or by other works in which the Apple
  27.                 Software may be incorporated.
  28.  
  29.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  30.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  31.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  33.                 COMBINATION WITH YOUR PRODUCTS.
  34.  
  35.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  36.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  37.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  39.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  40.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  41.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  
  43.     Copyright © 1993-2001 Apple Computer, Inc., All Rights Reserved
  44. */
  45.  
  46. #include "MacIncludes.h"
  47. #include "SimpleText.h"
  48.  
  49. #if defined(USE_UMBRELLA_HEADERS) && USE_UMBRELLA_HEADERS
  50. #include <QuickTime.h>
  51. #else
  52. #include "MovieFile.h"
  53. #endif
  54.  
  55. #if ALLOW_QUICKTIME
  56. // --------------------------------------------------------------------------------------------------------------
  57. static void DoFindInMovie(Movie theMovie, long *searchOffset, Boolean goBackwards)
  58. {
  59.     Track    searchTrack = nil;
  60.     OSStatus    anErr;
  61.     
  62.     if (goBackwards)
  63.         (*searchOffset)--;
  64.     else
  65.         (*searchOffset)++;
  66.  
  67.     anErr = MovieSearchText(theMovie, (Ptr)&gFindString[1], gFindString[0],
  68.                 (gWrapAround ? findTextWrapAround : 0) |
  69.                 (gCaseSensitive ? findTextCaseSensitive : 0) |
  70.                 findTextUseOffset |
  71.                 (goBackwards ? findTextReverseSearch : 0),
  72.                 &searchTrack, (TimeValue*)nil, searchOffset);
  73.                 
  74.     if (anErr)
  75.         SysBeep(1);
  76. } // DoFindInMovie
  77.  
  78. // --------------------------------------------------------------------------------------------------------------
  79.  
  80. static OSStatus    MovieAdjustMenus(WindowPtr pWindow, WindowDataPtr pData)
  81. {
  82. #pragma unused (pWindow)
  83.  
  84.     if (GetMovieIndTrackType( ((MovieDataPtr)pData)->theMovie, 1, 'text', movieTrackCharacteristic) != nil)
  85.         {
  86.         EnableCommand(cFind);
  87.         if (gFindString[0] != 0)
  88.             EnableCommand(cFindAgain);
  89.         }
  90.  
  91.     return noErr;
  92.     
  93. } // MovieAdjustMenus
  94.  
  95. // --------------------------------------------------------------------------------------------------------------
  96.  
  97. static OSStatus    MovieCommand(WindowPtr pWindow, WindowDataPtr pData, short commandID, long menuResult)
  98. {
  99. #pragma unused (pWindow, menuResult)
  100.  
  101.     OSStatus    anErr = noErr;
  102.     
  103.     switch (commandID)
  104.         {
  105.         case cFind:
  106.             if (ConductFindOrReplaceDialog(kFindWindowID) == cancel)
  107.                 break;
  108.         
  109.         case cFindAgain:
  110.             DoFindInMovie(((MovieDataPtr)pData)->theMovie, 
  111.                         &((MovieDataPtr)pData)->searchOffset,
  112.                         ((gEvent.modifiers & shiftKey) != 0));
  113.             break;
  114.         }
  115.     
  116.     return(anErr);
  117.     
  118. } // MovieCommand
  119.  
  120. // --------------------------------------------------------------------------------------------------------------
  121.  
  122. static OSStatus    MovieCloseWindow(WindowPtr pWindow, WindowDataPtr pData)
  123. {
  124. #pragma unused (pWindow)
  125.  
  126.     DisposeMovieController( ((MovieDataPtr)pData)->thePlayer);
  127.     DisposeMovie( ((MovieDataPtr)pData)->theMovie);
  128.     CloseMovieFile(pData->resRefNum);
  129.     pData->resRefNum = -1;
  130.     
  131.     return(noErr);
  132.     
  133. } // MovieCloseWindow
  134.  
  135. // --------------------------------------------------------------------------------------------------------------
  136. static OSStatus MovieAdjustCursor(WindowPtr pWindow, WindowDataPtr pData, Point *localMouse, RgnHandle globalRgn)
  137. {
  138. #pragma unused (pWindow, pData, globalRgn, localMouse)
  139.     
  140.     return(eActionAlreadyHandled);
  141.     
  142. } // MovieAdjustCursor
  143.  
  144. // --------------------------------------------------------------------------------------------------------------
  145.  
  146. static OSStatus    MovieGetBalloon(WindowPtr pWindow, WindowDataPtr pData, 
  147.         Point *localMouse, short * returnedBalloonIndex, Rect *returnedRectangle)
  148. {
  149. #pragma unused (pWindow, pData, localMouse, returnedRectangle)
  150.  
  151.     *returnedBalloonIndex = iDidTheBalloon;
  152.     
  153.     return(noErr);
  154.     
  155. } // MovieGetBalloon
  156.  
  157. // --------------------------------------------------------------------------------------------------------------
  158.  
  159. static Boolean    MovieFilterEvent(WindowPtr pWindow, WindowDataPtr pData, EventRecord *pEvent)
  160. {
  161. #pragma unused (pWindow)
  162.     
  163.     return(MCIsPlayerEvent( ((MovieDataPtr)pData)->thePlayer, pEvent));
  164. } // MovieFilterEvent
  165.  
  166. // --------------------------------------------------------------------------------------------------------------
  167.  
  168. static long MovieCalculateIdleTime(WindowPtr pWindow, WindowDataPtr pData)
  169. {
  170. #pragma unused (pWindow, pData)
  171.  
  172.     if (!IsMovieDone( ((MovieDataPtr)pData)->theMovie))
  173.         return(0);
  174.     else
  175.         return(0x7FFFFFFF);
  176.         
  177. } // MovieCalculateIdleTime
  178.  
  179. // --------------------------------------------------------------------------------------------------------------
  180.  
  181. static OSStatus    MovieMakeWindow(WindowPtr pWindow, WindowDataPtr pData)
  182. {
  183.     OSStatus                anErr;
  184.     short                actualResId;
  185.     Movie                theMovie;
  186.     MovieController        thePlayer;
  187.     Rect                movieBounds;
  188.     long                version;
  189.     
  190.     Gestalt(gestaltQuickTime, &version);
  191.     
  192.     pData->pAdjustMenus            = (AdjustMenusProc)            MovieAdjustMenus;
  193.     pData->pCommand                = (CommandProc)                MovieCommand;
  194.     pData->pCloseWindow         = (CloseWindowProc)            MovieCloseWindow;
  195.     pData->pFilterEvent         = (FilterEventProc)            MovieFilterEvent;
  196.     pData->pGetBalloon             = (GetBalloonProc)            MovieGetBalloon;
  197.     pData->pCalculateIdleTime    = (CalculateIdleTimeProc)    MovieCalculateIdleTime;
  198.     pData->pAdjustCursor        = (AdjustCursorProc)        MovieAdjustCursor;
  199.     pData->dragWindowAligned    = (version >= 0x01508000);
  200.     
  201.     actualResId = DoTheRightThing;
  202.  
  203.     // close down other paths to the file -- because we don't need them
  204.     if (pData->resRefNum != -1)
  205.         {
  206.         CloseResFile(pData->resRefNum);
  207.         pData->resRefNum = -1;
  208.         }
  209.     if (pData->dataRefNum != -1)
  210.         {
  211.         FSClose(pData->dataRefNum);
  212.         pData->dataRefNum = -1;
  213.         }
  214.  
  215.     anErr = OpenMovieFile(&pData->fileSpec, &pData->resRefNum, 0);
  216.     if (anErr == noErr)
  217.         {
  218.         anErr = NewMovieFromFile(&theMovie, pData->resRefNum, &actualResId, (unsigned char *) 0, newMovieActive, (Boolean *) 0);
  219.         if (anErr == noErr)
  220.             anErr = GetMoviesError();
  221.         
  222.         // leave slop because some movies/VR don't work with low mem, but eat it anyway
  223.         if (FreeMem() < kRAMNeededForNew)
  224.             {
  225.             DisposeMovie(theMovie);
  226.             anErr = memFullErr;
  227.             }
  228.         }
  229.     else
  230.         {
  231.         pData->resRefNum = -1;
  232.         }
  233.     nrequire(anErr, NewMovieFromFile);
  234.     
  235.     // position the movie
  236.     GetMovieBox(theMovie, &movieBounds);
  237.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  238.     SetMovieBox(theMovie, &movieBounds);
  239.     OffsetRect(&movieBounds, pData->contentRect.left, pData->contentRect.top);
  240.  
  241.     // make it draw in the correct window
  242.     SetMovieGWorld(theMovie, (CGrafPtr) pWindow, 0);
  243.     thePlayer = NewMovieController(theMovie, &movieBounds, mcTopLeftMovie);
  244.     anErr = GetMoviesError();
  245.     if ((anErr == noErr) && (!thePlayer)) anErr = memFullErr;
  246.     nrequire(anErr, NewMovieFromFile);
  247.     MCGetControllerBoundsRect(thePlayer, &movieBounds);
  248.  
  249.     // make sure the window is the proper size
  250.     SizeWindow(pWindow, movieBounds.right - movieBounds.left,
  251.                             movieBounds.bottom - movieBounds.top, false);
  252.     pData->contentRect.right = pData->contentRect.left + 
  253.         movieBounds.right - movieBounds.left;
  254.     pData->contentRect.bottom = pData->contentRect.top + 
  255.         movieBounds.bottom - movieBounds.top;
  256.     if (pData->dragWindowAligned)
  257.         AlignWindow((WindowPtr)pWindow, false, nil, nil);
  258.     
  259.     // enable keyboard events
  260.     MCDoAction(thePlayer, mcActionSetKeysEnabled, (void*)1);
  261.     
  262.     // Save a reference to the movie
  263.     ((MovieDataPtr)pData)->theMovie = theMovie;
  264.     ((MovieDataPtr)pData)->thePlayer = thePlayer;
  265.     ((MovieDataPtr)pData)->searchOffset = 0;
  266.     
  267. // FALL THROUGH EXCEPTION HANDLING
  268. NewMovieFromFile:
  269.     return(anErr);
  270. } // MovieMakeWindow
  271.  
  272. #endif
  273.  
  274. // --------------------------------------------------------------------------------------------------------------
  275.  
  276. OSStatus    MoviePreflightWindow(PreflightPtr pPreflightData)
  277. {    
  278. #if ALLOW_QUICKTIME
  279.     pPreflightData->continueWithOpen     = true;
  280.     pPreflightData->makeProcPtr         = MovieMakeWindow;
  281.     pPreflightData->resourceID            = kMovieWindowID;
  282.     pPreflightData->storageSize         = sizeof(MovieDataRecord);
  283. #endif
  284.  
  285.     return(noErr);
  286.     
  287. } // MoviePreflightWindow
  288.  
  289. // --------------------------------------------------------------------------------------------------------------
  290.  
  291. void MovieGetFileTypes(OSType * pFileTypes, OSType * pDocumentTypes, short * numTypes)
  292. {
  293. #if ALLOW_QUICKTIME
  294.     if (gMachineInfo.haveQuickTime)
  295.         {
  296.         pFileTypes[*numTypes]         = 'MooV';
  297.         pDocumentTypes[*numTypes]     = kMovieWindow;
  298.         (*numTypes)++;
  299.         }
  300. #endif
  301. } // MovieGetFileTypes
  302.